import http, {ApiResponse, ApiResponsePage} from "../../../../lang/http";

export interface DictConfig {
    // ID
    id: string;
    // 库名
    schemaName: string;
    // 表名
    tableName: string;
    // 表别名
    tableAlias: string;
    // 列名
    columnName: string;
    // 列别名
    columnAlias: string;
    // 默认样式
    defaultStyle: string;
    // 是否可编辑
    editable: boolean;
}

const formModel = (): DictConfig => {
    return {
        //ID
        id: ''
        //库名
        , schemaName: 'med'
        //表名
        , tableName: ''
        //表别名
        , tableAlias: ''
        //列名
        , columnName: ''
        //列别名
        , columnAlias: ''
        //默认样式
        , defaultStyle: ''
        //是否可编辑
        , editable: true
    }
}

const formRules = {
    id: [
        {required: true, message: "[ID]字段不允许为空"}
    ],
    schemaName: [
        {min: 0, max: 32, message: "[库名]最多64字符"}
    ],
    tableName: [
        {min: 0, max: 32, message: "[表名]最多64字符"}
    ],
    tableAlias: [
        {min: 0, max: 32, message: "[表别名]最多32字符"}
    ],
    columnName: [
        {min: 0, max: 32, message: "[列名]最多64字符"}
    ],
    columnAlias: [
        {min: 0, max: 32, message: "[列别名]最多32字符"}
    ],
    defaultStyle: [
        {min: 0, max: 32, message: "[默认样式]最多32字符"}
    ],
}



/**
 * 新建数据字典配置
 * @param params 数据字典配置参数
 * @returns 请求Promise
 */
const create = (params: object): Promise<ApiResponse> => {
    return http.ajax({
        method: 'POST',
        url: '/sys/dic/config/info',
        params
    });
}

/**
 * 删除数据字典配置
 * @param arg 字段信息
 * @returns 请求Promise
 */
const remove = (arg: object): Promise<ApiResponse> => {
    return http.ajax({
        method: "DELETE",
        url: 'sys/dic/config/info',
        params: arg
    });
}

/**
 * 快速编辑字典配置
 * @param arg 字段信息
 * @returns 请求Promise
 */
const modify = (arg: object): Promise<ApiResponse> => {
    return http.ajax({
        method: "POST",
        url: 'sys/dic/config/edit',
        params: arg
    });
}

/**
 * 查询数据字典列表
 * @param params 查询参数
 * @returns 分页数据Promise
 */
const page = (params: object): Promise<ApiResponsePage> => {
    return http.ajax({
        url: '/sys/dic/config/page',
        method: 'GET',
        params
    });
}

export default {create, remove, modify, page, formModel, formRules}